<?php

namespace Business\Comment;

/**
 * 评论列表业务
 */
class ListModel extends \Business\AbstractModel {

    /**
     * 评论列表
     * 
     * @param array $params
     * @return array
     */
    public function commentList($params) {
        $this->_checkParams($params);
        $where = $this->_getWhere($params);
        $order = $this->_getOrder($params);

        $commentDAO = \DAO\Comment\CommentModel::getInstance();
        $result     = $commentDAO->fetchAll(null
                , $where
                , $order
                , $params["count"]
                , $params["offset"]);
        $result     = $this->_adapterModels($result, $params["need_fields"]);

        if ($params["need_first"]) {
            return current($result);
        }

        $return = array("listdata" => $result, 'page' => $params['page'], 'count' => $params['count']);
        if ($params["need_total"] == 1) {
            $return['total'] = $this->total($params);
        }
        return $return;
    }

    /**
     * 校验参数
     * 
     * @param array $params
     */
    protected function _checkParams(&$params) {
        $form = new \Forms\Comment\ListModel($params);
        if (!$form->validate()) {
            \Error\ErrorModel::throwException(110010, current($form->getMessages()));
        }
        $params = $form->getFieldValue();

        $params['offset'] = ($params["page"] - 1) * $params["count"];

        $params['main_need_fields']    = array("*");
        $params['adapter_need_fields'] = $params['need_fields'];

        return $params;
    }

    /**
     * 获取where条件
     * 
     * @param array $params
     * @return array
     */
    protected function _getWhere($params) {
        $where = array();
        if (!empty($params["source_id"])) {
            $where['source_id'] = $params["source_id"];
        }
        return $where;
    }

    /**
     * 获取排序字段
     * 
     * @param array $params
     * @return array
     */
    protected function _getOrder($params) {
        $order = array("add_time desc");
        return $order;
    }

    /**
     * 计算总数
     * 
     * @param array $params
     * @return int
     */
    public function total($params) {
        $where  = $this->_getWhere($params);
        $DAO    = \DAO\Comment\CommentModel::getInstance();
        $result = $DAO->fetchAll(array("total" => new \Zend\Db\Sql\Expression("count(0)")), $where, null, 1);
        return $result[0]["total"];
    }

    /**
     * 适配页面输出
     * 
     * @param array $models
     * @return array
     */
    protected function _adapterModels($models, $needFields) {
        $result = array();
        foreach ($models as $model) {
            $result[] = $this->_adapterModel($model, $needFields);
        }
        return $result;
    }

    /**
     * 适配输出
     * 
     * @params $model
     * @params $needFields
     * @return array
     */
    protected function _adapterModel($model, $needFields) {
        return $model;
    }

    /**
     * 登录业务
     * 
     * @var \Business\Comment\ListModel
     */
    private static $_instance = null;

    /**
     * 单例模式获取类实例
     * 
     * @return \Business\Comment\ListModel
     */
    public static function getInstance() {
        if (!(self::$_instance instanceof self)) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

}
